#include "app_motor.h"

/////////////////////////////////////////////////////////////////////////////////////////////////////////
#define APP_MOTOR_CTRL_PIN          LL_GPIO_PIN_6
#define APP_MOTOR_CTRL_PORT         GPIOB

#define MOTOR_RUNNING               (0x00)
#define MOTOR_STOP                  (0x01)

unsigned short value; 

typedef struct {
    void (*cb) (void);              // 电机转动检测转动回调
    void (*result) (void);          // 出粮检测回调
    void (*jam) (void);             // 检测堵转回调
    unsigned char running;
    unsigned char counter;
} motor_mgr_t;

volatile static motor_mgr_t sg_motor_mgr;

static void app_motor_detect_init(void)
{
    LL_TIM_InitTypeDef TIM_InitStruct = {0x00};
    LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_TIM14);
    TIM_InitStruct.ClockDivision        = LL_TIM_CLOCKDIVISION_DIV1;
    TIM_InitStruct.CounterMode          = LL_TIM_COUNTERMODE_UP;
    TIM_InitStruct.Prescaler            = 24 - 1;
    TIM_InitStruct.Autoreload           = 1000 - 1;//10000 - 1; // 10ms中断一次
    TIM_InitStruct.RepetitionCounter    = 0;
    LL_TIM_Init(TIM14, &TIM_InitStruct);
    NVIC_SetPriority(TIM14_IRQn,0);
}

static void app_motor_detect_io_init(void)
{
    LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);

    LL_GPIO_InitTypeDef GPIO_InitStruct = {0x00};
    GPIO_InitStruct.Pin         = APP_MOTOR_CTRL_PIN;
    GPIO_InitStruct.Mode        = LL_GPIO_MODE_OUTPUT;
    GPIO_InitStruct.Speed       = LL_GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.OutputType  = LL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.Pull        = LL_GPIO_PULL_DOWN;
    LL_GPIO_Init(APP_MOTOR_CTRL_PORT, &GPIO_InitStruct);

    sg_motor_mgr.running = 0x00;
}

static void app_motor_detect_start(void)
{
    LL_TIM_SetCounter(TIM14, 0x00);
    LL_TIM_EnableIT_UPDATE(TIM14);
    LL_TIM_EnableCounter(TIM14);
    NVIC_EnableIRQ(TIM14_IRQn);
}

static void app_motor_detect_stop(void)
{
    NVIC_DisableIRQ(TIM14_IRQn);
    LL_TIM_DisableCounter(TIM14);
    LL_TIM_DisableIT_UPDATE(TIM14);
    LL_TIM_ClearFlag_UPDATE(TIM14);
}

static void app_motor_ir_tx_start(void)
{
    LL_GPIO_SetOutputPin(APP_MOTOR_CTRL_PORT, APP_MOTOR_CTRL_PIN);
}

static void app_motor_ir_tx_stop(void)
{
    LL_GPIO_ResetOutputPin(APP_MOTOR_CTRL_PORT, APP_MOTOR_CTRL_PIN);
}

void app_motor_init(void)
{
    drv_motor_init();
    // app_motor_detect_init();
    // app_motor_detect_start();
    app_motor_detect_io_init();
}

void app_motor_cb_register(void (*cb)(void))
{
    sg_motor_mgr.cb = cb;
}

void app_motor_forward(void)
{
    sg_motor_mgr.counter = 0x00;
    drv_motor_forward();

    sg_motor_mgr.running = 0x01;
}

void app_motor_reverse(void)
{
    sg_motor_mgr.counter = 0x00;
    drv_motor_reverse();
    sg_motor_mgr.running = 0x01;
}

void app_motor_stop(void)
{
    drv_motor_stop();
    sg_motor_mgr.running = 0x00;
}

void app_motor_standby(void)
{
    drv_motor_standby();
    sg_motor_mgr.running = 0x00;
}

unsigned char app_motor_is_stopped(void)
{
    return drv_motor_is_stop();
}


void app_motor_release_result_cb_register(void (*cb)(void))
{
    sg_motor_mgr.result = cb;
}

void app_motor_jam_cb_register(void (*cb)(void))
{
    sg_motor_mgr.jam = cb;
}

void app_motor_task(void)
{
    if (sg_motor_mgr.running == 0x00) {
        return ;
    }

    if (sg_motor_mgr.counter < 0x0A) {	
        sg_motor_mgr.counter += 1;
        return ;
    }

    // 转动检测
    if (drv_release_sw_read_level() == 0) 
    {
        if (sg_motor_mgr.cb) 
        {
            sg_motor_mgr.cb();
            sg_motor_mgr.counter = 0x00;
        }
    }

    // 电机堵转检测
    // value = app_adc_convert(LL_ADC_CHANNEL_0);    
    // if (value > 300) {
    //     if (sg_motor_mgr.jam) {
    //         sg_motor_mgr.jam();
    //     }
    // }
}